//: \+. -L+snn

import
	tango.stdc.stringz,
	tango.io.Stdout,
	luac;

lua_State* L;

void log(T...)(T t){
	Stdout.formatln(t);
}

void err(T...)(T t){
	Stdout.formatln(t).flush;
	throw new Exception("");
}


extern(C) int test1 (lua_State *L) {
	int len = lua_gettop(L) ;
	log("len= {}", len);
	for (int i = 1; i <= len ; i++) {
		if( lua_isstring( L, i) ) {
			auto str	=  lua_tostring( L, i) ;
			log("\nstr= {}", fromStringz(str) );
		}
	}
	// return 
	lua_pushinteger(L, 8 ) ;
	return 1;
}

void main(){
	int ret ;
	L	= lua_open();
	assert(L !is null);
	luaL_openlibs(L);
	
	lua_register(L, "test1\0".ptr, &test1 );
	
	ret =  luaL_loadfile(L,"test.lua") ;
	if( ret !is 0 ) {
		err("luaL_loadfile failed");
		return ;
	}
	ret = lua_pcall(L,0,0,0) ;
	if ( ret !is 0 ) {
		err("lua_pcall failed: {} ",lua_tostring(L,-1) ) ;
	}
	
	lua_getglobal(L, "test\0".ptr ) ;
	
}